package com.kk.controller;

import com.kk.common.Result;
import com.kk.model.User;
import com.kk.service.UserService;
import com.kk.util.ToolUtils;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: kevin
 * @date: 2018-10-15 09:57
 */
@Controller //此处带有spring-mvc页面跳转，只能使用Controller，使用RestController会返回数据给前端
@RequestMapping("/mgr")
public class UserMgrController {
    private static final String PREFIX = "system/user/";

    @Autowired
    private UserService userService;

    @RequestMapping("")
    public String mgr() {

        return PREFIX + "user";
    }

    @PostMapping("/list")
    @ResponseBody
    public List<User> list(@RequestParam(required = false) String name) {
        return userService.selectUsers(name);
    }

    @RequestMapping("/user_add")
    public String userAdd() {
        return PREFIX + "user_add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Map<String, String> map) {
        User user = new User();
        user.setAccount(map.get("account"));
        user.setName(map.get("name"));
        user.setPassword(map.get("password"));
        if (user != null) {
            if (!userService.saveUser(user)) {
                throw new RuntimeException("添加用户失败");
            }
            return Result.success(user);
        }
        return Result.error(HttpStatus.FORBIDDEN, "接收到的user为null");

    }

    @GetMapping("/user_edit/{id}")
    public String userEdit(@PathVariable String id, Model model) {
        if (StringHelper.isNotEmpty(id)) {
            User user = userService.findUserById(id);
            model.addAttribute("user", user);
        }
        return PREFIX + "user_edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public Result userEdit(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        if (StringHelper.isNotEmpty(id)) {
            User user = userService.findUserById(id);
            if (user != null) {
                String account = map.get("account");
                if (StringHelper.isNotEmpty(account)) {
                    user.setAccount(account);
                }
                String name = map.get("name");
                if (StringHelper.isNotEmpty(name)) {
                    user.setName(name);
                }
                boolean bool = userService.saveUser(user);
                if (bool) {
                    return Result.success(user);
                }
            }
            return Result.error("400", "user为空");
        }
        return Result.error("400", "id为空");
    }


    @PostMapping("/rest")
    @ResponseBody
    public Result reset(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        if (StringHelper.isNotEmpty(id)) {
            User user = userService.findUserById(id);
            if (user != null) {
                user.setPassword("111111");
                boolean bool = userService.saveUser(user);
                if (bool) {
                    return Result.success(user);
                }
            }
            return Result.error("400", "user为空");
        }
        return Result.error("400", "id为空");
    }

    //角色分配页面
    @RequestMapping("/role_assign/{userId}")
    public String roleAssign(@PathVariable String userId, Model model) {
        if (ToolUtils.isEmpty(userId)) {
            throw new ServiceException("请求参数为空");
        }
        User user = this.userService.findUserById(userId);
        model.addAttribute("userId", userId);
        model.addAttribute("userAccount", user.getAccount());
        return PREFIX + "user_roleassign";
    }
}

